<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>老师信息</title>
    <style>
        #container {
            width: 80%;
            margin: 10px auto;
        }

        .teacher {
            width: 100%;
            margin: 0 auto;
            padding: 10px 0;
            border-bottom: 1px dashed gray;
            overflow: auto;
        }

        .teacher > div {
            float: left;
        }

        .photo {
            height: 140px;
            border-radius: 75px;
            overflow: hidden;
            margin-left: 20px;
        }

        .info {
            width: 75%;
            margin-left: 30px;
        }

        .info div {
            clear: both;
            margin: 5px 10px;
        }

        .info span {
            margin-right: 25px;
        }

        .info a {
            text-decoration: none;
            color: darkcyan;
        }
    </style>
</head>
<body>
<div id="container">
    <h1>{{ subject.name }}学科的老师信息</h1>
    <hr>
    {% if not teachers %}
        <h2>暂无该学科老师信息</h2>
    {% endif %}
    {% for teacher in teachers %}
        <div class="teacher">
            <div class="photo">
                <img src="/static/images/{{ teacher.photo }}" height="140" alt="">
            </div>
            <div class="info">
                <div>
                    <span><strong>姓名：{{ teacher.name }}</strong></span>
                    <span>性别：{{ teacher.sex | yesno:'男,女' }}</span>
                    <span>出生日期：{{ teacher.birth }}</span>
                </div>
                <div class="intro">{{ teacher.intro }}</div>
                <div class="comment">
                    <a id="good" href="/praise/?tno={{ teacher.no }}" class="action-link">好评</a>&nbsp;&nbsp;
                    (<strong id="gcount">{{ teacher.gcount }}</strong>)
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <a id="bad" href="/criticize/?tno={{ teacher.no }}" class="action-link">差评</a>&nbsp;&nbsp;
                    (<strong id="bcount">{{ teacher.bcount }}</strong>)
                </div>
            </div>
        </div>
    {% endfor %}
    <a href="/">返回首页</a>
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
    {% comment %}      document.getElementById("good").addEventListener('click', function (event){
              alert(event.)
          });

          document.getElementById("bad").addEventListener('click', function (event){

          });{% endcomment %}

    // 选择所有的带有 'action-link' 类的链接
    document.querySelectorAll('.action-link').forEach(function (link) {
        // 为每个链接添加点击事件监听器
        link.addEventListener('click', function (event) {
            // 阻止链接的默认行为
            event.preventDefault();
            // 获取链接的 href 属性值
            var url = this.getAttribute('href');
            // 使用 fetch API 发送 GET 请求
            fetch(url)
                .then(function (response) {
                    // 处理响应数据
                    if (!response.ok) {
                        throw new Error('Network response was not ok: ' + response.statusText);
                    }
                    // 解析 JSON 响应
                    return response.json();
                })
                .then(function (data) {
                    // 在这里处理返回的数据
                    console.log(data)
                    if (data.code == 20000) {
                        //$(evt.target).next().text(json.count)
                        var count = data.count;

                        var tabId = '#gcount'
                        if (url.startsWith("/criticize")) {
                           tabId = '#bcount'
                        }
                        // 获取页面中对应的 <strong> 标签
                        var strongTag = document.querySelector(tabId); // 替换 '#your-strong-tag-id' 为实际的 ID

                        // 更新 <strong> 标签的内容
                        if (strongTag) {
                            strongTag.textContent = count;
                        } else {
                            console.error('No <strong> tag found with the provided ID.');
                        }
                    } else {
                        alert(data.mesg)
                    }
                })
                .catch(function (error) {
                    // 处理请求过程中发生的错误
                    console.error('There has been a problem with your fetch operation:', error);
                });

            // 可以在这里添加更多的逻辑，比如发送 AJAX 请求等
        });
    });
    {% comment %}    $(() => {
            $('.comment>a').on('click', (evt) => {
                   alert(1111111);
                   console.log("11111");
                evt.preventDefault()
                alert(22222);
                let url = $(evt.target).attr('href')
                $.getJSON(url, (json) => {
                    if (json.code == 20000) {
                        //$(evt.target).next().text(json.count)
                        alert(3333);
                         window.location.reload();
                    } else {
                        alert(json.mesg)
                    }
                })
            })
        }){% endcomment %}
</script>
</body>
</html>